#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

void swap(char* buf1, char* buf2, int width)
{
	for (int i = 0; i < width; i++)
	{
		char tmp = *(buf1+i);
		*(buf1+i) = *(buf2+i);
		*(buf2+i) = tmp;
	}
}

void qsort(void* base, int sz, int width, int  (*cmp)(void* e1, void* e2))
{
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		int j = 0;
		for ( j = 0; j < sz - 1- i; j++)
		{
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
			{
				swap((char*)base + j * width, (char*)base + (j + 1) * width,width);
			}
		}
	}
}

//struct stu
//{
//	char name[10];
//	int age;
//};
//int cmp_stu_age(const void* a,const void* b)
//{
//	return ((struct stu*)a)->age - ((struct stu*)b)->age;
//}
//int cmp_stu_name(const void* a, const void* b)
//{
//	return strcmp(((struct stu*)a)->name, ((struct stu*)b)->name);
//}
//int test3(void* a, void* b)
//{
//	struct stu s[3] = { {"zhangsan",20},{"wangwu",18},{"wusi",22} };
//	int sz = sizeof(s) / sizeof(s[0]);
//	qsort(s, sz, sizeof(struct stu), cmp_stu_age);
//}


int cmp(const void* a,const void* b)
{
	return ((int)(*(int*)a - *(int*)b));
}
void test1()
{
	int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(int), cmp);
	for (int i = 0; i < 10; i++)
	{
		printf("%-3d ", arr[i]);
	}
	printf("\n");
}

int cmp2(const void *a,const void *b)
{
	return *(float*)a - *(float*)b;
}
void test2()
{
	float arr[] = { 1.2,5.6,9.6,4.3,2.1,7.1,9.1 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(float), cmp2);
	for (int i = 0; i < sz; i++)
	{
		printf("%.1f  ", arr[i]);
	}
	printf("\n");
}

int main()
{
	test1();
	
	test2();

	return 0;
}